﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Medical_Store_Billing.Component;
using System.Windows.Forms;
using Medical_Store_Billing.Object_Class;

namespace Medical_Store_Billing.Data_Access_Layer
{
    public class NhanvienDAL
    {
        Dataservice m_nhanviendal = new Dataservice();

        public DataTable Laydsnhanvien(string user)
        {
            SqlCommand cmd = new SqlCommand("Select * from nhanvien where userid = @user");
            cmd.Parameters.Add("user", SqlDbType.VarChar).Value = user;
            m_nhanviendal.Load(cmd);
            return m_nhanviendal;
        }

        public DataTable Timnv(string name)
        {
            SqlCommand cmd = new SqlCommand("Select * from nhanvien where tennv like N'%"+name+"%'");
            m_nhanviendal.Load(cmd);
            return m_nhanviendal;
        }

        public DataTable Laydsnhanvien1()
        {
            SqlCommand cmd = new SqlCommand("Select * from nhanvien order by manv ASC");
            m_nhanviendal.Load(cmd);
            return m_nhanviendal;
        }

        public bool Themnv(nhanvienclass m_nv)
        {
            SqlCommand cmd = new SqlCommand("Insert into nhanvien values(@manv,@tennv,@sdt,@user,@pass)");
            cmd.Parameters.Add("manv", SqlDbType.VarChar).Value = m_nv.manv;
            cmd.Parameters.Add("tennv", SqlDbType.NVarChar).Value = m_nv.tennv;
            cmd.Parameters.Add("sdt", SqlDbType.VarChar).Value = m_nv.sodt;
            cmd.Parameters.Add("user", SqlDbType.VarChar).Value = m_nv.user;
            cmd.Parameters.Add("pass", SqlDbType.VarChar).Value = m_nv.pass;

            if (m_nhanviendal.ExecuteNonQuery(cmd) > 0)
            {
                MessageBox.Show("Thêm nhân viên thành công!");
                return true;
            }
            else return false;
        }

        public bool Suanv(nhanvienclass m_nv)
        {
            SqlCommand cmd = new SqlCommand("Update nhanvien set tennv = @tennv, sodt = @sdt, userid = @user, pass = @pass where manv = @manv");
            cmd.Parameters.Add("manv", SqlDbType.VarChar).Value = m_nv.manv;
            cmd.Parameters.Add("tennv", SqlDbType.NVarChar).Value = m_nv.tennv;
            cmd.Parameters.Add("sdt", SqlDbType.VarChar).Value = m_nv.sodt;
            cmd.Parameters.Add("user", SqlDbType.VarChar).Value = m_nv.user;
            cmd.Parameters.Add("pass", SqlDbType.VarChar).Value = m_nv.pass;

            if (m_nhanviendal.ExecuteNonQuery(cmd) > 0)
            {
                MessageBox.Show("Sửa nhân viên thành công!");
                return true;
            }
            else return false;
        }
    }

    
}
